﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.CRM.BigCustomerTradeRecord;

namespace BCData.CRM.BigCustomerTradeRecord
{
    public class BigCustomerTradeRecordData : IBigCustomerTradeRecordData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.BigCustomerTradeRecordId,
		    A.BigCustomerId,
		    A.CompanyId,
		    A.UserId,
		    A.AdminId,
		    A.TradeOrderId,
		    A.TradeOrderTitle,
		    A.TradeAmount,
		    A.TradeTime,
		    A.TradeContractNo,
		    A.TradeSatisfaction,
		    A.HasComplaint,
		    A.HasRepair,
		    A.HasReturnGoods,
		    A.ReturnGoodsCount,
		    A.ReturnGoodsAmount,
		    A.RepairAmount,
		    A.RepairCause,
		    A.RepairEndTime,
		    A.RepairAdminId,
		    A.RepairAdminName,
		    A.IsUsRepairCause,
		    A.IsSettled,
		    A.IsReturnGoods,
		    A.IsRefund,
		    A.ReturnGoodsTime,
		    A.ReturnGoodsSignTime,
		    A.ReturnGoodsSignAdminId,
		    A.ReturnGoodsSignAdminName,
		    A.CreateTime,
		    A.CreateAdminId,
		    A.CreateAdminName,
		    A.UpdateTime,
		    A.EditAdminId,
		    A.EditAdminName,
        FROM
            BigCustomerTradeRecord A";



        #region 构造函数
        public BigCustomerTradeRecordData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新大客户交易记录纪录
        /// </summary>
        /// <param name="bigCustomerTradeRecordEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeRecordEntity> InsertAsync(BigCustomerTradeRecordEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改大客户交易记录记录
        /// </summary>
        /// <param name="bigCustomerTradeRecordEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeRecordEntity> UpdateAsync(BigCustomerTradeRecordEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除大客户交易记录记录
        /// </summary>
        /// <param name="bigCustomerTradeRecordId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long bigCustomerTradeRecordId)
        {
            string sql = "DELETE FROM crm_bigcustomertraderecord WHERE BigCustomerTradeRecordId = ?bigCustomerTradeRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerTradeRecordId", bigCustomerTradeRecordId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取大客户交易记录记录
        /// </summary>
        /// <param name="bigCustomerTradeRecordId"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeRecordEntity> GetEntityAsync(long bigCustomerTradeRecordId)
        {
            string sql = QUERY + " WHERE  A.BigCustomerTradeRecordId = ?bigCustomerTradeRecordId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerTradeRecordId", bigCustomerTradeRecordId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<BigCustomerTradeRecordEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询大客户交易记录记录
        public async Task<(IEnumerable<BigCustomerTradeRecordEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  crm_bigcustomertraderecord  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<BigCustomerTradeRecordEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



